#ifndef PINQMODEL_H
#define PINQMODEL_H

#include <e32base.h>
#include "PinQCategory.h"
#include "PinQTask.h"

class RWriteStream;
class RReadStream;

class CPinQModel : CBase
	{
public:
	enum TBackup {EBackup,ENoBackup};
	static CPinQModel* NewL();
	~CPinQModel();
	static inline TUid NewTaskUid() {return TUid::Null();}

	void RemoveTask(TInt aIndex,TBackup aBackup);
	void RemoveTask(TUid aUid, TBackup aBackup);
	TUid UpdatePrefL(TUid uid);
	
	CPinQTask& RecoverDeletedTaskL();
	inline TBool DeletedTaskExist() const
		{return iDeletedTask != NULL;}
	CPinQTask& Task(TUid aUdi) const;
	inline CPinQTask& Task(TInt aIndex) const
		{ return *(iPinQTasks[aIndex]); }
	inline CPinQTask::CPref& Pref(TUid aUid) 
		{ return aUid==NewTaskUid()?iTempPref:Task(aUid).Pref(); }
	inline CPinQTask::CPref& Pref(TInt aIndex) const
		{ return Task(aIndex).Pref(); }
	inline TInt TaskCount() {return iPinQTasks.Count();}
	CPinQTask* RunningTask() const;
	void StoreL( RWriteStream& aStream ) const;
	void RestoreL( RReadStream& aStream );

	inline TBool IsDirty() const {return iDirty;}
	inline void SetDirty(TBool aDirty){iDirty = aDirty;}
	
	void AddCategory(const TQikCategoryName& aCategoryName, TInt& aHandle);
	inline void AddCategory(const TPinQCategory& aCategory)
		{
		iPinQCategories.Append(aCategory);
		iDirty = ETrue;
		}
	void RenameCategory(const TInt aHandle, const TDesC& aNewName);
	void RemoveCategory(const TInt aHandle);
	
	inline TInt CategoryCount() const
		{
		return iPinQCategories.Count();
		}
	inline TPinQCategory& Category(const TInt aIndex) 
		{
		return iPinQCategories[aIndex];
		}

	void CategoryRemoved(TInt aHandle);
	TBool CategoryExists(TInt aHandle);
	
private:
	TUid NewUniqueUid();

	CPinQModel();
	void ConstructL();
	TInt FindTaskIndexFromPinQUid(const TUid& aUid) const;

private:
	CPinQTask::CPref iTempPref;
	RPointerArray<CPinQTask> iPinQTasks;
	CPinQTask* iDeletedTask;
	TBool iDirty;
	RArray<TPinQCategory> iPinQCategories;
	};
// vim: ts=2:sw=2
#endif
